<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/users' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" :body-style="{ padding: '10px'}">
      <!--警告栏-->
      <el-alert title="只允许为第三分类商品设置分类" type="info" show-icon></el-alert>
      <span>请选择商品分类：</span>
      <!--v-model="selectKeys" 选择的节点 :options="parentCate"绑定的数据 :props="cateProps"要显示的项目 -->
      <el-cascader
        v-model="selectKeys"
        :options="parentCate"
        :props="cateProps"
        @change="handleChange"
        :clearable="true"
        class="selectkey"
      ></el-cascader>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="only">
          <el-button type="primary" :disabled="isdisabled" size="mini">添加参数</el-button>
          <!--动态参数-->
          <el-table :border="true" :data="onlyCate">
            <!--展开栏-->
            <el-table-column type="expand" width="50">
              <template slot-scope="scope">
                <el-row>
                  <el-tag
                    :key="i"
                    v-for="(item,i) in scope.row.attr_vals"
                    closable
                    class="cateTags"
                    @close="deleteTag(scope.row,i)"
                  >{{item}}</el-tag>
                  <!--x新增tag 按钮和文本框的切换 @keyup.enter.native 按下回车键触发 @blur失去焦点-->
                  <el-input
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                    class="input-new-tag"
                  ></el-input>
                  <el-button v-else class="cateTags" size="small" @click="showInput">+ New Tag</el-button>
                </el-row>
              </template>
            </el-table-column>
            <el-table-column type="index" width="50"></el-table-column>
            <el-table-column prop="attr_name" label="产品名称" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  @click="edit(scope.row)"
                >编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="many">
          <el-button type="primary" :disabled="isdisabled" size="mini">添加参数</el-button>
          <!--静态参数-->
          <el-table :border="true" :data="manyCate">
            <el-table-column type="expand" width="50"></el-table-column>
            <el-table-column type="index" width="50"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  @click="edit(scope.row)"
                >编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
export default {
  created() {
    this.getParentCate();
  },
  data() {
    return {
      selectKeys: [],
      parentCate: [],
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover"
      },
      cateId: "",
      //动态/静态参数获取
      isdisabled: true,
      activeName: "only",
      onlyCate: [],
      manyCate: [],
      inputVisible: false,
      inputValue: ""
    };
  },
  methods: {
    async getParentCate() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 3 }
      });
      if (res.meta.status != 200) return this.$message.error("获取数据失败");
      this.parentCate = res.data;
    },
    //导航栏切换
    handleChange() {
      if (this.selectKeys.length > 0) {
        this.isdisabled = false;
      }
      if (this.selectKeys.length == 3) {
        this.cateId = this.selectKeys[2];
      }
      if (this.activeName == "only") this.getOnlyCate();
      else if (this.activeName == "many") this.getManyCate();
    },
    //按钮和文本框且切换
    handleClick() {
      if (this.activeName == "only") this.getOnlyCate();
      else if (this.activeName == "many") this.getManyCate();
    },
    //获取静态参数
    async getOnlyCate() {
      const {
        data: res
      } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: { sel: this.activeName }
      });
      if (res.meta.status != 200) return this.$message.error("请求数据失败");
      this.onlyCate = res.data;
      this.onlyCate.forEach(item => {
        item.attr_vals = item.attr_vals ? item.attr_vals.split(" ") : [];
      });
    },
    //获取动态参数
    async getManyCate() {
      const {
        data: res
      } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: { sel: this.activeName }
      });
      if (res.meta.status != 200) return this.$message.error("请求数据失败");
      this.manyCate = res.data;
    },
    edit(id) {},
    //增加tag
    handleInputConfirm(row) {
      let inputValue = this.inputValue.trim();
      if (inputValue) {
        row.attr_vals.push(inputValue);
        this.saveTagInput(row);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    //删除tags
    deleteTag(row, i) {
      row.attr_vals.splice(i, 1);
      this.saveTagInput(row);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    async saveTagInput(row) {
      const { data: res } = await this.$http.put(
        `categories/${this.cateId}/attributes/${row.attr_id}`,
        {
          attr_name: row.attr_name,
          attr_sel: row.attr_sel,
          attr_vals: row.attr_vals.join(" ")
        }
      );
      if (res.meta.status != 200) return this.$message.error("数据有误");
      this.$message.success("修改成功");
    }
  }
};
</script>
<style lang="less" scope>
.box-card {
  text-align: left;
}
.selectkey {
  margin-top: 10px;
  width: 30%;
}
.cateTags {
  margin: 5px;
}
.el-row .input-new-tag {
  width: 100px;
}
</style>